package com.code.zg.news.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.code.zg.news.app.App;
import com.code.zg.news.di.component.ActivityComponent;
import com.code.zg.news.di.component.DaggerActivityComponent;
import com.code.zg.news.di.module.ActivityModule;
import com.code.zg.news.utils.manager.ActivityManager;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;

public abstract  class BaseActivity<T extends BasePresenter> extends SupportActivity implements BaseView {
    @Inject
    protected T mPresenter;
    @Inject
    protected ActivityManager activityManager;
    private Unbinder unbinder;
    protected Activity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        unbinder = ButterKnife.bind(this);
        mContext=this;
        initInject();
//        if(mPresenter!=null){
            mPresenter.attchView(this);
//        }
        activityManager.addActivity(this);

        initEventAndData();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        if(mPresenter!=null){
            mPresenter.detachView();
        }
        activityManager.removeActivity(this);
    }

    @Override
    public void useNightMode(boolean isNight) {
         AppCompatDelegate.setDefaultNightMode(isNight?AppCompatDelegate.MODE_NIGHT_YES:AppCompatDelegate.MODE_NIGHT_NO);
        recreate();
    }
    protected void setToolBar(Toolbar toolBar,String title){
        toolBar.setTitle(title);
        setSupportActionBar(toolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressedSupport();
            }
        });
    }
    //获取activityComponent
    protected ActivityComponent getAtivityComponent(){
        return DaggerActivityComponent.builder()
                .appComponent(App.getAppComponent())
                .activityModule(new ActivityModule(this))
                .build();
    }

    protected abstract int getLayout();
    protected abstract void initInject();
    protected abstract void initEventAndData();
}
